package com.awesumgames.testgame;

import org.json.JSONArray;
import org.json.JSONObject;

import tv.ouya.console.api.OuyaController;

import com.awesumgames.awesum.Game;
import com.awesumgames.awesum.Layer3d;
import com.awesumgames.awesum.Vector3;
import com.awesumgames.awesum.AwesumGlobal.Awesum;
import com.awesumgames.awesum.SceneCollection.SceneMode;
//import com.awesumgames.awesum.loaders.collada.Collada;
import com.awesumgames.awesum.controls.Control.Ouya;

public class BreakoutLayer extends Layer3d {
	public static final short COLL_NONE = 0x00000000;
    public static final short COLL_BALL = 0x00000001;
    public static final short COLL_BLOCK = 0x00000002;
    public static final short COLL_ITEM = 0x00000004;
    public static final short MASK_BALL = COLL_BALL | COLL_BLOCK;
    public static final short MASK_BLOCK = COLL_BALL;
    public static final short MASK_ITEM = COLL_BALL;
	
	private int _level = 0;
	private String _levelName = "";
	public Paddle paddle = null;
	public Ball ball = null;
	public Vector3 test = null;

	public BreakoutLayer(int levelFile) {
		_level = levelFile;
	}
	
	public void load() {
		empty();
		//Awesum.debug("bl load");
		
		JSONObject level = null;
		try {
			level = Awesum.Load.Json(_level);
			if (level != null) {
				JSONArray jArr = null;
				JSONObject jObj = null;
				JSONArray pos = null;
				
				_levelName = level.getString("name");  
				
				//BLOCK
				jArr = level.getJSONArray("block");
				for (int i = 0; i < jArr.length(); ++i) {
					jObj = jArr.getJSONObject(i);
					
					pos = jObj.getJSONArray("pos");
					String t = jObj.getString("type");
					Block.Type type;
					
					if (t.equals("b")) type = Block.Type.BOOST;
					else if (t.equals("sb")) type = Block.Type.SUPERBOOST;
					else if (t.equals("r")) type = Block.Type.REDUCE;
					else if (t.equals("br")) type = Block.Type.BREAK;
					else if (t.equals("s")) type = Block.Type.SLIDE;
					else type = Block.Type.NORMAL;
					add(new Block(type,
							new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))));
				}

				//PADDLE
				jArr = level.getJSONArray("paddle");
				for (int i = 0; i < jArr.length(); ++i) {
					jObj = jArr.getJSONObject(i);
					pos = jObj.getJSONArray("pos");
					String t = jObj.getString("type");
					Paddle.Type type;
					
					if (t.equals("r")) type = Paddle.Type.REVERSE;
					else if (t.equals("hl")) type = Paddle.Type.HALF_LEFT;
					else if (t.equals("hm")) type = Paddle.Type.HALF_MIDDLE;
					else if (t.equals("hr")) type = Paddle.Type.HALF_RIGHT;
					else if (t.equals("m")) type = Paddle.Type.MINI;
					else type = Paddle.Type.NORMAL;
					add(paddle = new Paddle(type,
							new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))));
				}
				
				//BALL
				jArr = level.getJSONArray("ball");
				for (int i = 0; i < jArr.length(); ++i) {
					//Log.d("debug", "added ball");
					jObj = jArr.getJSONObject(i);
					pos = jObj.getJSONArray("pos");
					add(ball = new Ball(new Vector3(pos.getDouble(0), pos.getDouble(1), pos.getDouble(2))));
				}
				test = ball.pos;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//Game.phys.addGround(0f);
		Game.phys.addPlane(new Vector3(), new Vector3(0f, 1f, 0f));
		Game.phys.addPlane(new Vector3(-.5f, 0f, 0f), new Vector3(1f, 0f, 0f)); //left
		Game.phys.addPlane(new Vector3(10f, 0f, 0f), new Vector3(-1f, 0f, 0f)); //right
		
		super.load();
		
		Game.camera.set(4.5f,	3f,	10f,
				4.5f,	3f,	-5f,
				0f,	1f,	0f);
		Game.camera.view = test;
	}
	
	public void draw() {
		Game.Shaders.use("color");
		super.draw();
	}
	
	public void step() {
		super.step();
		/*if (ball!=null) {
			if (ball.pos.y > 4f) {
				Game.camera.view.y = ball.pos.y - 1f;
				//game.camera.pos.z = 10 + ball.origin.y - 4;
				float overage = ball.pos.y - 4f;
				Vector3 newPos = Game.camera.pos.copy();
				newPos.z = 10 + overage * .5f;
				newPos.y = 3f - overage * 2f;
				Game.camera.moveTo(newPos, 5f);
			} else {
				//game.camera.view.y = 2f;
				Vector3 newPos = new Vector3(4.5f,	3f,	10f);
				Game.camera.moveTo(newPos, 3f);
			}
		}*/
		
		if (Ouya.justPressed(0, 1)) {
			ball.phys.body.applyCentralImpulse(new Vector3(-10f, 0f, 0f));
		} else if (Ouya.justPressed(0, 2)) {
			ball.phys.body.applyCentralImpulse(new Vector3(10f, 0f, 0f));
		}
		if (Ouya.bO[0]) {
			ball.phys.body.applyCentralImpulse(new Vector3(0f, 10f, 0f));
		}
	}

}
